<?php
/**
 * @user:andy
 * @date:2021/6/14
 * @company:http://www.sjdphp.com
 * @desc:
 */

namespace app\common\controller;


use app\BaseController;
use app\common\middleware\CheckCurrent;
use app\common\server\Json;
use app\common\server\Tree;

class BackBaseController extends BaseController
{
    protected $model_id     = '';
    protected $model_name   = '';
    protected $model_cate   = '';
    protected $model_desc   = '';
    protected $model_status = '';

    protected function initialize()
    {
        $this->middleware = [
            \app\common\middleware\CheckCurrent::class,
            \app\common\middleware\CheckAdminToken::class,
            \app\common\middleware\CheckOperateLog::class,
            \app\common\middleware\CheckAdminMenu::class
        ];
    }


    public function initMenu(){
        if($this->request->isGet()){
            ## 拼接原始的菜单
            $param = [
                'homeInfo' => [
                    'title' => '欢迎您',
                    'href'  => '/back/index/welcome',
                ],
                'logoInfo' => [
                    'title' => '来学呀',
                    'image' => '/static/storage/favicon.ico',
                    'href'  => '/back',
                ],
            ];
            $param["menuInfo"] = Tree::getMenuTree($this->request->MenuInit);
            return \json($param);
        }else{
            return Json::error('请求方式错误');
        }
    }

    /** 通用化获取列表
     * @return \think\response\View
     * @throws \ReflectionException
     */
    public function lists(){
        ## 收参
        $page = $this->request->param("page",0,'intval');
        $limit = $this->request->param("limit",0,'intval');
        $searchParams = json_decode($this->request->param("searchParams"),true);

        ## 默认查询数据的状态
        $term[] = [$this->model_status,'<>',config("project.mysql_code.table_delete")];

        ## 判断是否存在搜索数据
        if(!empty($searchParams)){
            if(isset($searchParams['name']) && $searchParams['name'] !=''){
                $term[] = [$this->model_name,'like','%'.$searchParams['name'].'%'];
            }
            if(isset($searchParams["cate_id"]) && is_numeric($searchParams['cate_id'])){
                $term[] = [$this->model_cate,'=',$searchParams['cate_id']];
            }
        }

        ## 获取实例
        $class = self::getBusExample($this->request->controller());
        $res = $class::getLists($term,'',$page,$limit,$this->model_desc);

        ## 判断请求方式并返回数据
        if($this->request->isPost()){
            return $res;
        }

        return view();
    }

    /** 通用化添加数据
     * @return \think\response\Json|\think\response\View
     * @throws \ReflectionException
     */
    public function add(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();

            ## 获取验证层实例对象
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 获取默认的状态
            $param[$this->model_status] = config("project.mysql_code.table_pending");
            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param);

            return $res;
        }else{
            ## 加载模版
            return view();
        }
    }

    /** 通用化编辑数据
     * @return \think\response\Json|\think\response\View
     * @throws \ReflectionException
     */
    public function edit(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();

            ## 获取验证层实例对象
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("edit")->check($param)) return Json::error($validate->getError);


            ## 获取操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param,2);

            return $res;
        }else{
            ## 接受需要编辑的主键ID
            $id = $this->request->param($this->model_id,0,'intval');
            if(!empty($id)){
                ## 查询单条数据
                $class = self::getBusExample($this->request->controller());
                $info = $class::getInfo(array($this->model_id => $id));
            }else{
                $info = array();
            }

            ## 加载模版
            return view('',[
                'info' => $info
            ]);
        }
    }

    /** 通用化删除数据
     * @return \think\response\Json
     * @throws \ReflectionException
     */
    public function del(){
        if($this->request->isGet()){
            ## 接受参数
            $id = $this->request->param($this->model_id,0,'intval');
            if(!empty($id)){
                ## 需要修改的数据
                $param = array(
                    $this->model_id => $id,
                    $this->model_status => config("project.mysql_code.table_delete"),
                    'operator'    => $this->request->BackInfo["back_name"]
                );

                ## 获取实例对象并执行
                $class = self::getBusExample($this->request->controller());
                return $class::insertOrUpdateAndDel($param,3);
            }else{
                return Json::error("参数不能为空");
            }
        }else{
            return Json::error("请求方式错误");
        }
    }


    /** 通用化修改状态
     * @return \think\response\Json
     * @throws \ReflectionException
     */
    public function status(){
        if($this->request->isPost()){
            ## 接受参数并校验
            $id = $this->request->param($this->model_id,'intval');
            if(empty($id)) return Json::error("参数错误");

            ## 查询数据
            $class = self::getBusExample($this->request->controller());
            $info = $class::getInfo(array($this->model_id => $id));

            ## 要修改的信息
            $param = array(
                $this->model_id => $id,
                $this->model_status => $info[$this->model_status] == config("project.mysql_code.table_normal") ? config("project.mysql_code.table_pending") : config("project.mysql_code.table_normal"),
                'operator'    => $this->request->BackInfo["back_name"]
            );

            return $class::insertOrUpdateAndDel($param,2);
        }else{
            return Json::error("请求方式错误");
        }
    }


    /** 反射获取业务层或验证层
     * @param $controller
     * @param $type
     * @return object
     * @throws \ReflectionException
     */
    protected static function getBusExample($controller,$type = 1){
        if($type == 1){
            $path = "app\admin\business\\" . $controller . 'Bus';
        }else{
            $path = "app\admin\\validate\\" . $controller . 'Validate';
        }

        return (new \ReflectionClass($path))->newInstance();
    }
}